﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using System.Data.Linq;
using System.Configuration;
using System.Diagnostics;

namespace DomainModel.Concrete
{
    public class DetailRepository:IDetailRepository
    {
        private Table<Detail> detailTable;
        private DataContext dataContext;

        public DetailRepository()
        {
            
            String cs = ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString();
            dataContext = new DataContext(cs);
            detailTable = dataContext.GetTable<Detail>();
        }

        public IQueryable<Detail> Detail
        {
            get { return detailTable; }
        }

        public Detail GetDetail(Int32 id)
        {
            try
            {
                return detailTable.First(d => d.DetailID == id);
            }
            catch (Exception exc)
            {
                return null;
            }
        }
        
        public IQueryable<Detail> GetDetails(Int32 orderid)
        {
            try
            {
                return detailTable.Where(d => d.rf_OrderID == orderid);
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public void Add(Detail d)
        {
            try
            {
                detailTable.InsertOnSubmit(d);
            }
            catch (Exception exc)
            {

            }
        }

        public void Delete(Detail d)
        {
            detailTable.DeleteOnSubmit(d);
        }

        public void Save()
        {
            detailTable.Context.SubmitChanges();
            detailTable.Context.Refresh(RefreshMode.OverwriteCurrentValues, detailTable);
            Repositories.VOrderRepository.RefreshContext();
        }
    }
}
